package
        com.zzc.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hos.entity.stock.Medicines;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import com.zzc.sale.feigns.UserFeign;
import com.zzc.sale.mapper.MedicinesMapper;
import com.zzc.sale.service.MedicinesService;
import com.zzc.sale.vo.MedicinesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * @author：xIAO_ZHOU_TX
 * @date：Created in 2023/12/12 10:16
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class MedicinesServiceImpl implements MedicinesService {
    @Resource
    private MedicinesMapper medicinesMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserFeign userFeign;



    @Override
    public Page<Medicines> findMedicinesByCond(MedicinesVo medicinesVo) {
        Page<Medicines> page = new Page<>(medicinesVo.getPageNum(), medicinesVo.getPageSize());
        LambdaQueryWrapper<Medicines> wrapper = new LambdaQueryWrapper<>();
        // 药品名称
        if (StringUtils.hasText(medicinesVo.getMedicinesName())) {
            wrapper.like(Medicines::getMedicinesName, medicinesVo.getMedicinesName());
        }
        // 关键字
        if (StringUtils.hasText(medicinesVo.getKeywords())) {
            wrapper.like(Medicines::getKeywords, medicinesVo.getKeywords());
        }
        // 生产厂商ID
        if (Objects.nonNull(medicinesVo.getProducterId())) {
            wrapper.eq(Medicines::getProducterId, medicinesVo.getProducterId());
        }
        // 状态
        if (Objects.nonNull(medicinesVo.getStatus())) {
            wrapper.eq(Medicines::getStatus, medicinesVo.getStatus());
        }
        // 药品类型
        if (Objects.nonNull(medicinesVo.getMedicinesType())) {
            wrapper.eq(Medicines::getMedicinesType, medicinesVo.getMedicinesType());
        }
        // 处方类型
        if (Objects.nonNull(medicinesVo.getPrescriptionType())) {
            wrapper.eq(Medicines::getPrescriptionType, medicinesVo.getPrescriptionType());
        }
        // 删除状态
        if (Objects.nonNull(medicinesVo.getDelFlag()) && medicinesVo.getDelFlag() == 0) {
            wrapper.eq(Medicines::getDelFlag, medicinesVo.getDelFlag());
        }
        //创建时间
        if (Objects.nonNull(medicinesVo.getDateRange())) {
            wrapper.between(Medicines::getCreateTime, medicinesVo.getDateRange()[0], medicinesVo.getDateRange()[1]);
        }

        return medicinesMapper.selectPage(page, wrapper);
    }


    @Override
    public List<Medicines> findMedicinesAll() {
        return medicinesMapper.selectList(null);
    }

    @Override
    public Integer addMedicines(Medicines medicines) {
        // 创建时间 ==>添加时间
        medicines.setCreateTime(new Date());
        //  获取创建者信息
        String token = WebUtil.getToken();
        String userName = userFeign.selectUserByPhone(redisTemplate.opsForValue().get("user:" + token)).getUserName();
        medicines.setCreateBy(userName);
        return medicinesMapper.insert(medicines);
    }



    @Override
    public Integer updateMedicines(Medicines medicines) {
        // 更新时间
        medicines.setUpdateTime(new Date());
        // 获取更新者信息
        String token = WebUtil.getToken();
        String userName = userFeign.selectUserByPhone(redisTemplate.opsForValue().get("user:" + token)).getUserName();
        medicines.setCreateBy(userName);
        return medicinesMapper.updateById(medicines);
    }

    @Override
    public Medicines getMedicinesById(Long medicinesId) {
        Medicines medicines = medicinesMapper.selectById(medicinesId);
        return medicines;
    }

    @Override
    public Integer deleteMedicinesByIds(List<Long> ids) {
        int i = medicinesMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public Integer updateMedicinesStorage(Long medicinesId, Integer medicinesStockNum) {
        return medicinesMapper.updateMedicinesStorage(medicinesId, medicinesStockNum);

    }

    @Override
    public boolean reducePharmaceuticalById(Medicines medicines) {
        // 获取 药品id
        Long medicinesId = medicines.getMedicinesId();
        // 查询药品库存
        Medicines selectById = this.medicinesMapper.selectById(medicinesId);
        Integer stockNum = selectById.getMedicinesStockNum();
        if (stockNum >= medicines.getMedicinesStockNum()&&selectById!=null) {
            Integer num = stockNum - medicines.getMedicinesStockNum();
            //  进行减少库存
            LambdaUpdateWrapper<Medicines> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Medicines::getMedicinesId, medicinesId)
                    .set(Medicines::getMedicinesStockNum, num);
            this.medicinesMapper.update(updateWrapper);
        } else {
            throw new RuntimeException("药品:" + medicinesId + "库存数量不足");
        }
        return true;
    }

    @Override
    public boolean addPharmaceuticalById(Medicines medicines) {
        // 获取 药品id
        Long medicinesId = medicines.getMedicinesId();
        // 查询药品库存
        Medicines selectById = this.medicinesMapper.selectById(medicinesId);
        if (selectById != null) {
            Integer stockNum = selectById.getMedicinesStockNum();
            // 进行添加库存
            Integer num = stockNum + medicines.getMedicinesStockNum();
            LambdaUpdateWrapper<Medicines> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Medicines::getMedicinesId, medicinesId)
                    .set(Medicines::getMedicinesStockNum, num);
            this.medicinesMapper.update(updateWrapper);
            return true;
        }
        return false;
    }
}